package com.nsgf.library.ui.mine;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.kunminx.binding_state.BR;
import com.nsgf.library.data.bean.AddressBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityAddressListBinding;
import com.zhht.htzx.databinding.ActivityInviterListBinding;
import com.zhht.htzx.databinding.ItemAddressListBinding;
import com.zhht.htzx.databinding.ItemInviterBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的邀请列表
 */
public class InviterListActivity extends BaseActivity {

    private StateHolder mStates;

    private AccountRequester mAccountRequester;

    private SimpleDataBindingAdapter<AddressBean, ItemInviterBinding> mAdapter;

    private AddressBean addressBean;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    private ActivityInviterListBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<AddressBean, ItemInviterBinding>(this,
                R.layout.item_inviter, new DiffUtil.ItemCallback<AddressBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull AddressBean oldItem, @NonNull AddressBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull AddressBean oldItem, @NonNull AddressBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemInviterBinding binding, AddressBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
            }
        };
        return new DataBindingConfig(R.layout.activity_inviter_list, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mBinding = (ActivityInviterListBinding) getBinding();
        mStates.titleBar.set("我的邀请");

        getLifecycle().addObserver(mAccountRequester);
        mAccountRequester.getAddressListResult().observe(this, new Observer<ResponseStatus<List<AddressBean>>>() {
            @Override
            public void onChanged(ResponseStatus<List<AddressBean>> listResponseStatus) {
                if (listResponseStatus.getResponseCode() == 0) {

                    mStates.inviterList.set(listResponseStatus.getData());
                }
                mBinding.smartRefreshLayout.finishRefresh();
            }
        });

        mBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                mAccountRequester.getInviterList();
            }
        });

    }


    @Override
    protected void onResume() {
        super.onResume();
        mBinding.smartRefreshLayout.autoRefresh();
    }

}
